<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\db\MainPage;

/**
 * MainPageQuery represents the model behind the search form about `\app\models\db\MainPage`.
 */
class MainPageQuery extends MainPage
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['mini_programs_id', 'create_datetime', 'id'], 'safe'],
            [['hot_goods_num', 'newest_goods_num', 'recommend_goods_num', 'free_express_goods_num', 'promotion_goods_num', 'hot_goods_sort', 'newest_goods_sort', 'recommend_goods_sort', 'free_express_goods_sort', 'promotion_goods_sort'], 'integer'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = MainPage::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'hot_goods_num' => $this->hot_goods_num,
            'newest_goods_num' => $this->newest_goods_num,
            'recommend_goods_num' => $this->recommend_goods_num,
            'free_express_goods_num' => $this->free_express_goods_num,
            'promotion_goods_num' => $this->promotion_goods_num,
            'hot_goods_sort' => $this->hot_goods_sort,
            'newest_goods_sort' => $this->newest_goods_sort,
            'recommend_goods_sort' => $this->recommend_goods_sort,
            'free_express_goods_sort' => $this->free_express_goods_sort,
            'promotion_goods_sort' => $this->promotion_goods_sort,
            'create_datetime' => $this->create_datetime,
        ]);

        $query->andFilterWhere(['like', 'mini_programs_id', $this->mini_programs_id])
            ->andFilterWhere(['like', 'id', $this->id]);

        return $dataProvider;
    }
}
